<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/smarty/plugins/function.img.php, $Author:  $ */

/** Display image
 *
 *
 * @coauthor Alexey Ovsyannikov aka
 */
function smarty_function_img($aParams, &$oSmarty)
{
    if (!isset($aParams['src']))
    {
        $oSmarty->trigger_error('Smarty: {img} - missed "src" param', E_USER_ERROR, __FILE__, __LINE__);
    }

    $sPath = _smarty_img_get_path($aParams['src']);
    if (!$sPath)
    {
        return _smarty_img_make_tag($aParams);
    }

    $bFileExists = false;
    if (file_exists($sPath))
    {
        $bFileExists = true;
    }

    if (!$bFileExists && isset($aParams['src_def']))
    {
        $sPath = _smarty_img_get_path($aParams['src_def']);
        if (!$sPath)
        {
            $aParams['src'] = $aParams['src_def'];
            return _smarty_img_make_tag($aParams);
        }
        elseif (file_exists($sPath))
        {
            $bFileExists = true;
            $aParams['src'] = $aParams['src_def'];
            if (isset($aParams['def_height']))
            {
                $aParams['height'] = $aParams['def_height'];
            }
            if (isset($aParams['def_width']))
            {
                $aParams['width'] = $aParams['def_width'];
            }
        }
    }

    if (!$bFileExists && isset($aParams['src_def2']))
    {
        $sPath = _smarty_img_get_path($aParams['src_def2']);
        if (!$sPath)
        {
            $aParams['src'] = $aParams['src_def2'];
            return _smarty_img_make_tag($aParams);
        }
        elseif (file_exists($sPath))
        {
            $bFileExists = true;
            $aParams['src'] = $aParams['src_def2'];
        }
    }

    if ($bFileExists && $sPath && !(isset($aParams['height']) && isset($aParams['width'])))
    {
        $aImInfo = @getimagesize($sPath);
        $aParams['height'] = isset($aParams['height']) ? $aParams['height'] : $aImInfo[1];
        $aParams['width']  = isset($aParams['width']) ? $aParams['width'] : $aImInfo[0];
    }

    if ( ereg('file/pic/gallery/thumb/', $sPath) )
    {
    	$aParams['max_height'] = App::getParam('upload_set_size_thumb');
    	$aParams['max_width'] = App::getParam('upload_set_size_thumb');
    }
    
    if (App::getParam('bImageSecurity') && strpos($aParams['src'], '/file/pic/gallery/thumb/'))
    {
    	$aParts = explode('thumb/', $aParams['src']);
    	if (isset($aParts[1]))
    	{
	    	$aParts = explode('.', $aParts[1]);
	    	if (isset($aParts[0]) && is_numeric($aParts[0]))
	    	{
		    	App::setAccessForContent('gallery', $aParts[0]);
	    	}
    	}
    }
    
    if (App::getParam('bImageSecurity') && strpos($aParams['src'], '/file/pic/groups/gallery/thumb/'))
    {
    	$aParts = explode('thumb/', $aParams['src']);
    	if (isset($aParts[1]))
    	{
	    	$aParts = explode('.', $aParts[1]);
	    	if (isset($aParts[0]) && is_numeric($aParts[0]))
	    	{
		    	App::setAccessForContent('groups', $aParts[0]);
	    	}
    	}
    }    

    if (isset($aParams['max_height']) || isset($aParams['max_width']))
    {
        list($aParams['height'], $aParams['width']) = _smarty_img_recalc_size($aParams);
    }
    return _smarty_img_make_tag($aParams);
}

function _smarty_img_get_path($sUrl)
{
    $aParsed = parse_url($sUrl);
    if (!isset($aParsed['scheme']) || !$aParsed['scheme'])
    {
        return false;
    }
    
    $sHost = App::getParam('host');
    $sSubFolder = substr($sHost, strpos($sHost, '/'));
    $aParsed['path'] = str_replace($sSubFolder, '/', $aParsed['path']);
    return App::getParam('path').ltrim($aParsed['path'], '/');
}

function _smarty_img_make_tag($aParams)
{
    $aSpecialParams = array
    (
        'src_def',
        'src_def2',
        'def_width',
        'def_height',
        'max_width',
        'max_height',
        'add_stamp'
    );
    $aParams['src'] = str_replace(' ', '%20', $aParams['src']);

    //add default alt
    if (!isset($aParams['alt']))
    {
        $sAlt = basename($aParams['src']);
        $iPos = strrpos($sAlt, '.');
        if ($iPos !== false)
        {
	        $sAlt = substr($sAlt, 0, $iPos);
        }
        $aParams['alt'] = $sAlt;
    }

    if (isset($aParams['height']) && !intval($aParams['height']))
    {
        unset($aParams['height']);
    }
    if (isset($aParams['width']) && !intval($aParams['width']))
    {
        unset($aParams['width']);
    }
    if (isset($aParams['height']))
    {
        $aParams['height'] = intval($aParams['height']);
    }
    if (isset($aParams['width']))
    {
        $aParams['width'] = intval($aParams['width']);
    }
    if (isset($aParams['add_stamp']))
    {
        $aParams['src'] = $aParams['src'] . '?id=' . time();
    }    

    $sRes = '<img';
    foreach($aParams as $k=>$v)
    {
        if (!in_array($k, $aSpecialParams))
        {
            $sRes .= ' '.$k.'="'.$v.'"';
        }
    }
    $sRes .= ' />';
    return $sRes;
}

/** Recalcuelate image size
 *
 * @param array $aParams pluginm aParams array
 * @return array(height, width)
 */
function _smarty_img_recalc_size($aParams)
{
	$k = "";

    $iHeight    = isset($aParams['height']) ? $aParams['height'] : 0;
    $iWidth     = isset($aParams['width']) ? $aParams['width'] : 0;
    $iMaxHeight = isset($aParams['max_height']) ? $aParams['max_height'] : 0;
    $iMaxWidth  = isset($aParams['max_width']) ? $aParams['max_width'] : 0;

    //get scaling factor
    if ($iMaxWidth && $iMaxHeight && $iWidth && $iHeight)
    {
        $kX = $iMaxWidth / $iWidth;
        $kY = $iMaxHeight / $iHeight;
        $k = min($kX, $kY);
    }
    elseif($iMaxHeight && $iHeight)
    {
        $k = $iMaxHeight / $iHeight;
    }
    elseif($iMaxWidth && $iWidth)
    {
        $k = $iMaxWidth / $iWidth;
    }

    //correct scaling factor
    if (((0 >= $k) || ($k > 1)))
    {
        $k = 1;
    }

    $iHeight *= $k;
    $iWidth *= $k;
    return array($iHeight, $iWidth);
}
?>